import wsgiref.handlers
from google.appengine.ext import webapp

import re
from datetime import datetime, timedelta

class Converter( webapp.RequestHandler ):

  def get( self ):
    self.response.headers['Content-Type'] = 'text/plain'

    try:
      date      = self.request.get('date') #dd/mm/yyyy
      time      = self.request.get('time') #hh:mm
      from_zone = self.request.get('from-zone')
      to_zone   = self.request.get('to-zone')

      date_split = re.split( r'/', date )
      dd    = int( date_split[0] )
      mm    = int( date_split[1] )
      yyyy  = int( date_split[2] )

      time_split = re.split ( r':', time )
      hour  = int( time_split[0] )
      min   = int( time_split[1] )

      from_split = re.split( r':', from_zone )
      if len(from_split) > 1 :
        from_hour_delta = int( from_split[0] )
        from_min_delta  = int( from_split[1] )
        if from_hour_delta < 0:
          from_min_delta *= -1
      else:
        from_hour_delta = int( from_split[0] )
        from_min_delta  = 0

      to_split = re.split( r':', to_zone )
      if len(to_split) > 1 :
        to_hour_delta   = int( to_split[0] )
        to_min_delta    = int( to_split[1] )
        if to_hour_delta < 0:
          to_min_delta *= -1
      else:
        to_hour_delta   = int( to_split[0] )
        to_min_delta    = 0

    except:
      self.response.out.write( 'error:\n  invalid parameter' )
      return

    fmt = '%Y-%m-%d %H:%M:%S'

    from_time   = datetime( yyyy, mm, dd, hour, min, 0 )
    utc_time    = from_time - timedelta( hours=from_hour_delta, minutes=from_min_delta )
    to_time     = utc_time + timedelta( hours=to_hour_delta, minutes=to_min_delta )

    self.response.out.write(
      '''
from: %(from_time)s (%(from)s)
utc: %(utc_time)s
to: %(to_time)s (%(to)s)
      '''.strip() % {
          'from_time' : from_time.strftime(fmt)
        , 'utc_time' : utc_time.strftime(fmt)
        , 'to_time' : to_time.strftime(fmt)
        , 'from' : from_zone
        , 'to' : to_zone
      }
    )
  # end of Converter.get

# end of class Converter
